﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;

/// <summary>
/// Summary description for BrokerPremiumDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class BrokerPremiumDB
    {
        public BrokerPremiumDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(BrokerPremium brokerpremium)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXCRMBrokerPremiumInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CXBrokerPremiumId", brokerpremium.CXBrokerPremiumId);
            _sPara.Add("@CXBrokerProviderId", brokerpremium.CXBrokerProviderId);
            _sPara.Add("@ProviderCoverTypeId", brokerpremium.ProviderCoverTypeId);
            _sPara.Add("@CECarId", brokerpremium.CECarId);
            _sPara.Add("@CustomerId", brokerpremium.CustomerId);
            _sPara.Add("@AgentId", brokerpremium.AgentId);
            _sPara.Add("@Premium", brokerpremium.Premium);
            _sPara.Add("@Excess", brokerpremium.Excess);
            _sPara.Add("@SelectedStatus", brokerpremium.SelectedStatus);
            _sPara.Add("@Package", brokerpremium.Package);
            _sPara.Add("@WorkShop", brokerpremium.WorkShop);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public Int64 Delete(Int64 CXBrokerPremiumId, Int64 AgentId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXBrokerPremiumDeleteSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CXBrokerPremiumId", CXBrokerPremiumId);
            _sPara.Add("@AgentId", AgentId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public DataSet GetBrokerPremium(Int64 CECarId, Int64 RefCEUserId)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXBrokerPremiumSelectItemList]";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarId", CECarId);
            _spara.Add("@RefCEUserId", RefCEUserId);

            DataSet _ds = _db.GetDataSet(_spName, _spara);

            return _ds;
        }

        public BrokerPremium GetBrokerPremium(Int64 CXBrokerProviderId, Int64 ProviderCoverTypeId, Int64 CECarId, Int64 CustomerId)
        {
            BrokerPremium _mybrokerpremium = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXBrokerPremiumSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@CXBrokerProviderId", CXBrokerProviderId);
            _spara.Add("@ProviderCoverTypeId", ProviderCoverTypeId);
            _spara.Add("@CECarId", CECarId);
            _spara.Add("@CustomerId", CustomerId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mybrokerpremium = new BrokerPremium();
                    _mybrokerpremium = FillDataRecord(_dt, 0);
                }
            }
            return _mybrokerpremium;
        }

        public BrokerPremium GetBrokerPremium(Int64 CXBrokerProviderId, Int64 ProviderCoverTypeId, Int64 CECarId, Int64 CustomerId, bool SelectedStatus)
        {
            BrokerPremium _mybrokerpremium = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXBrokerPremiumSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@CXBrokerProviderId", CXBrokerProviderId);
            _spara.Add("@ProviderCoverTypeId", ProviderCoverTypeId);
            _spara.Add("@CECarId", CECarId);
            _spara.Add("@CustomerId", CustomerId);
            _spara.Add("@SelectedStatus", SelectedStatus);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mybrokerpremium = new BrokerPremium();
                    _mybrokerpremium = FillDataRecord(_dt, 0);
                }
            }
            return _mybrokerpremium;
        }

        public DataSet GetCXCICRMQuotes(Int64 CECarId, Int64 RefCEUserId, string InsuranceType)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXCRMCIUserQuotesSelectedItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarId", CECarId);
            _spara.Add("@RefCEUserId", RefCEUserId);
            _spara.Add("@InsuranceType", InsuranceType);

            DataSet _ds = _db.GetDataSet(_spName, _spara);

            return _ds;
        }

        private BrokerPremium FillDataRecord(DataTable myDataTable, int RowId)
        {
            BrokerPremium myBrokerPremium = new BrokerPremium();
            myBrokerPremium.CXBrokerPremiumId = Convert.ToInt64(myDataTable.Rows[RowId]["CXBrokerPremiumId"].ToString());
            myBrokerPremium.CXBrokerProviderId = Convert.ToInt64(myDataTable.Rows[RowId]["CXBrokerProviderId"].ToString());
            myBrokerPremium.ProviderCoverTypeId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCoverTypeId"].ToString());
            myBrokerPremium.CECarId = Convert.ToInt64(myDataTable.Rows[RowId]["CECarId"].ToString());
            myBrokerPremium.CustomerId = Convert.ToInt64(myDataTable.Rows[RowId]["CustomerId"].ToString());
            myBrokerPremium.AgentId = Convert.ToInt64(myDataTable.Rows[RowId]["AgentId"].ToString());
            myBrokerPremium.Premium = Convert.ToSingle(myDataTable.Rows[RowId]["Premium"].ToString());
            myBrokerPremium.Excess = Convert.ToSingle(myDataTable.Rows[RowId]["Excess"].ToString());
            myBrokerPremium.SelectedStatus = Convert.ToBoolean(myDataTable.Rows[RowId]["SelectedStatus"].ToString());
            myBrokerPremium.CreatedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["CreatedDate"].ToString());
            myBrokerPremium.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            myBrokerPremium.Package = myDataTable.Rows[RowId]["Package"].ToString();
            myBrokerPremium.WorkShop = myDataTable.Rows[RowId]["WorkShop"].ToString();
            return myBrokerPremium;
        }

    }
}